#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct TreeNode {
	char data;
	struct TreeNode* left;
	struct TreeNode* right;
} TreeNode;

int pos;

TreeNode* createNode(char data) {
	TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
	node->data = data;
	node->left = NULL;
	node->right = NULL;
	return node;
}

TreeNode* parseTree(const char* str) {
	if (str[pos] == '\0' || str[pos] == ')') {
		return NULL;
	}

	while (str[pos] == ' ') pos++;

	if (str[pos] == '(' || str[pos] == ',') {
		return NULL;
	}

	TreeNode* node = createNode(str[pos]);
	pos++;

	while (str[pos] == ' ') pos++;

	if (str[pos] == '(') {
		pos++;
		node->left = parseTree(str);

		while (str[pos] == ' ') pos++;
		if (str[pos] == ',') {
			pos++;
			while (str[pos] == ' ') pos++;
			node->right = parseTree(str);
		}

		while (str[pos] == ' ') pos++;
		if (str[pos] == ')') {
			pos++;
		}
	}

	return node;
}

void preorderTraversal(TreeNode* root) {
	if (root == NULL) {
		return;
	}
	printf("%c ", root->data);
	preorderTraversal(root->left);
	preorderTraversal(root->right);
}

void inorderTraversal(TreeNode* root) {
	if (root == NULL) {
		return;
	}
	inorderTraversal(root->left);
	printf("%c ", root->data);
	inorderTraversal(root->right);
}

void postorderTraversal(TreeNode* root) {
	if (root == NULL) {
		return;
	}
	postorderTraversal(root->left);
	postorderTraversal(root->right);
	printf("%c ", root->data);
}

void freeTree(TreeNode* root) {
	if (root == NULL) {
		return;
	}
	freeTree(root->left);
	freeTree(root->right);
	free(root);
}

int main() {
	char str[1000];
	char queryValue;

	fgets(str, sizeof(str), stdin);
	str[strcspn(str, "\n")] = 0;

	pos = 0;
	TreeNode* root = parseTree(str);

	preorderTraversal(root);
	printf("\n");

	inorderTraversal(root);
	printf("\n");

	postorderTraversal(root);
	printf("\n");

	freeTree(root);

	return 0;
}